/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TShopMemberLevel;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TShopMemberLevelService;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BaseDialog;

/**
 * 说明：
 * 会员详细Dialog
 * @Author：李智深
 * @Date：2012-03-20
 */
public class MemberLevelPanel extends BaseDialog{

	private static final long serialVersionUID = 1L;
	
	private JButton okButton;//确定
	private JButton cancelButton;//关闭
	private JLabelJComboBoxComponent giftSelect = null; //选择优惠卷
	private TUserService tUserService;//用户操作 

	private TShopMemberLevelService	tShopMemberLevelService;
	//private TSiteGiftService tSiteGiftService;//赠送礼品
	List <TShopMemberLevel> tShopMemberLevel = new ArrayList();
	List<TUser> tUser = new ArrayList();
	public MemberLevelPanel(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		//tSiteGiftService = BuildSpringApplication.getInstance().getTSiteGiftServiceImpl();
		tShopMemberLevelService = BuildSpringApplication.getInstance().getTShopMemberLevelServiceImpl();
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
	}
	
	private void initViews(){
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);		
		main_jp.setBackground(Util.PANEL_COLOR);
		//List tCashCardID = new ArrayList();
		
		try {
			tShopMemberLevel = tShopMemberLevelService.findService(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		giftSelect=new JLabelJComboBoxComponent("请选择级别：",new String[]{},188);
		giftSelect.addElement(new JComboBoxPOJO("请选择……"));
		for(int i =0;i<tShopMemberLevel.size(); i++){
			giftSelect.addElement(new JComboBoxPOJO(tShopMemberLevel.get(i).getLevelID(), tShopMemberLevel.get(i).getName()+"", tShopMemberLevel.get(i).getName()));
		}
		//((JComboBoxPOJO)cashCardSelect.getBox().getSelectedItem()).getIdStr();
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("确定");
		cancelButton = new RPJButtonGray("关闭");
		jp1.add(okButton);
		jp1.add(cancelButton);
		jp1.setOpaque(false);
		
		main_jp.add(giftSelect);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	private void initListener(){
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Date date=new Date();
		Boolean isShow =false;
		if(e.getSource()==okButton){
			//确定
			if(giftSelect.getBox().getSelectedIndex() == 0){
				JOptionPane.showMessageDialog(null, "请选择级别。");
				return;
			}
			TShopMemberLevel tShopMemberLevel = new TShopMemberLevel();
			tShopMemberLevel.setLevelID(((JComboBoxPOJO)giftSelect.getBox().getSelectedItem()).getIdStr());
			for(int i = 0; i < tUser.size(); i++){
				try {
						tUser.get(i).setTShopMemberLevel(tShopMemberLevel);
						tUserService.saveOrUpdateService(tUser.get(i));
						isShow=true;

				} catch (Exception e1) {
					isShow=false;
					e1.printStackTrace();
				}
			}
			if(isShow){
				JOptionPane.showMessageDialog(null, "设置成功！");
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(null, "数据异常。");
			}
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	
	public void showDialog(List<TUser> user){
		for(int i =0;i<user.size();i++){
			tUser.add(user.get(i));
		}
		this.setVisible(true);
	}
	
}
